Inserting images dynamically.

by: tonyphoang, 9 years ago


Is there anyway to load images or videos (.mp4, gif) with flask? I've tried it with no success.

__init__.py


@app.route('/results-watch-movie/', methods=['POST'])
def resultsmovieaction(chartID = 'chart_ID', chart_type = 'line', chart_height = 500):
    try:
        mp4filename=request.form['mp4_result_selected']
        #connects dirpat of 'results_data' folder to 'csv_filename'
        complete_mp4_dirpath = results_data_dirpath+mp4filename
        #reads csv into a list
        return render_template("results-watch-mp4.html", mp4filename=mp4filename, complete_mp4_dirpath=complete_mp4_dirpath)
    except Exception as e:





{% extends "header.html" %}
{% block body %}

results-watch-mp4.html
<center>
The following video file file was selected : <b>{{mp4filename}}</b>
<p>
the file path is {{complete_mp4_dirpath}}
</p>


image below
<img src="/templates/test.mp4"/>
image above


</center>
{% endblock %}





{% extends "header.html" %}
{% block body %}


<body>

        <div id="container">
                <form method="POST" action="{{ url_for('resultsmovieaction') }}">  
                <center>
                  <br>Watch Video of Raw Data<br>
                    <select name="mp4_result_selected">
                      {% for m in mp4_datanames %}
                      <option value="{{m}}">{{m}}</option>
                      {% endfor %}
                    </select>
                    <br>
                    <br>
                  <input type="submit" value='Watch this Movie!'>
                  </center>
                </form>

        </div>



</body>
{% endblock %}





You must be logged in to post. Please login or register an account.



There are many ways to do something like this. Generally, after choosing the movie, you'd have something with URL converters.

More info on url converters: https://pythonprogramming.net/flask-url-converters-tutorial/

An example from stackoverflow: http://stackoverflow.com/questions/12034949/flask-how-to-get-url-for-dynamically-generated-image-file


-Harrison 9 years ago

You must be logged in to post. Please login or register an account.


Thanks got it working. Only problem is that the images don't show up. I think the only media that is viewable is through the static folder. Is there a way to get another local folder to view the images?

-tonyphoang 9 years ago

You must be logged in to post. Please login or register an account.